package com.bytes32.classification.tree.dataset;

import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;

import org.apache.hadoop.io.WritableUtils;
/**
 * Returns one single count for one value
 * of a feature
 * @author murariuf
 *
 */
public class DefaultFeatureProcessor implements FeatureProcessor<Entry<ByteArrayWrapper, Long>> {
	
	private ByteArrayWrapper value;
	
	@Override
	public void readFields(DataInput stream) throws IOException {
		this.value = new ByteArrayWrapper();
		this.value.readFields(stream);
	}

	@Override
	public void write(DataOutput stream) throws IOException {
		this.value.write(stream);
	}

	@Override
	public void handleString(String seq) throws InvalidFeatureValueException {
		if (seq == null)
			throw new InvalidFeatureValueException("No null features");
		value = new ByteArrayWrapper();
		value.setWrapped(seq.getBytes());
	}

	@Override
	public long getCount() {
		return 1L;
	}

	@Override
	public Iterator<Entry<ByteArrayWrapper, Long>> iterator() {
		return new Iterator<Entry<ByteArrayWrapper,Long>>() {

			private boolean hasNext = true;
			
			@Override
			public boolean hasNext() {
				return hasNext;
			}

			@Override
			public Entry<ByteArrayWrapper, Long> next() {
				hasNext = false;
				return new Entry<ByteArrayWrapper, Long>() {
					public Long setValue(Long value) { throw new UnsupportedOperationException(" Not supported "); }
					public Long getValue() { return 1L; }
					public ByteArrayWrapper getKey() { return DefaultFeatureProcessor.this.value; }
				};
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException(" Not supported ");
			}
		};
	}

	@Override
	public long getTotalCount() {
		return 1L;
	}
	
	@Override
	public void compact() {
		/* Nothing */
	}

}
